<?php
/*
stardevelop.com Live Help
International Copyright stardevelop.com

You may not distribute this program in any manner,
modified or otherwise, without the express, written
consent from stardevelop.com

You may make modifications, but only for your own 
use and within the confines of the License Agreement.
All rights reserved.

Selling the code for this program without prior 
written consent is expressly forbidden. Obtain 
permission before redistributing this program over 
the Internet or in any other medium.  In all cases 
copyright and header must remain intact.  
*/

$installed = false;
$database = include('./include/database.php');
if ($database) {
	include('./include/spiders.php');
	include('./include/class.mysql.php');
	$installed = include('./include/config.php');
	include('./include/version.php');
} else {
	$installed = false;
}

if ($installed == false) {
	include('./include/settings.php');
}

if (!isset($_REQUEST['COMPLETE'])){ $_REQUEST['COMPLETE'] = ''; }
if (!isset($_REQUEST['SECURITY'])){ $_REQUEST['SECURITY'] = ''; }
if (!isset($_REQUEST['BCC'])){ $_REQUEST['BCC'] = ''; }

$error = '';
$invalid_email = '';
$invalid_security = '';
$email = '';
$name = '';
$message = '';
$code = '';
$status = '';

if($_REQUEST['COMPLETE'] == true) {

	$name = htmlentities(trim(stripslashes($_REQUEST['NAME'])), ENT_NOQUOTES, 'UTF-8');
	$email = htmlentities(trim(stripslashes($_REQUEST['EMAIL'])), ENT_NOQUOTES, 'UTF-8');
	$message = htmlentities(trim(stripslashes($_REQUEST['MESSAGE'])), ENT_NOQUOTES, 'UTF-8');
	$code = trim($_REQUEST['SECURITY']);
	$bcc = htmlentities(trim(stripslashes($_REQUEST['BCC'])), ENT_NOQUOTES, 'UTF-8');;

	if ($email == '' || $name == '' || $message == '') {
		$error = true;
	}
	else {
	
		if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
					  '@'.
					  '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
					  '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $email)) {
					  $invalid_email = true;
		}
		else {
		
			$md5code = md5(strtoupper($code));
			if ($security != $md5code && $_SETTINGS['SECURITYCODE'] == true && ((function_exists('imagepng') || function_exists('imagejpeg')) && function_exists('imagettftext') && $security_code)) {
				$invalid_security = true;
				
				// Generate a NEW random string
				$chars = array('a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H','i','I','j','J','k','K','l','L','m','M','n','N','o','O','p','P','q','Q','r','R','s','S','t','T','u','U','v','V','w','W','x','X','y','Y','z','Z','1','2','3','4','5','6','7','8','9');
				$security = '';
				for ($i = 0; $i < 5; $i++) {
				   $security .= $chars[rand(0, count($chars)-1)];
				}
				
				$session = array();
				$session['REQUEST'] = $request_id;
				$session['SECURITY'] = md5(strtoupper($security));
				$session['LANGUAGE'] = LANGUAGE_TYPE;
				$data = serialize($session);
					
				setCookie('LiveHelpSession', $data, false, '/', $cookie_domain, 0);

			}
			else {
				$country = 'Unavailable';
				$url = 'Unavailable';
				$title = 'Unavailable'; 
				$referrer = 'Unavailable';
				
				$query = "SELECT `url`, `title`, `referrer` FROM " . $table_prefix . "requests WHERE `id` = '$request_id'";
				$row = $SQL->selectquery($query);
				if (is_array($row)) {
					$url = $row['url'];
					$title = $row['title'];
					$referrer = $row['referrer'];
					
					if ($url == '') { $url = 'Unavailable'; }
					if ($title == '') { $title = 'Unavailable'; }
					if ($referrer == '') { $referrer = 'Unavailable'; } elseif ($referrer == 'false') { $referrer = 'Direct Link / Bookmark'; }
				}
				
				if ($_SETTINGS['IP2COUNTRY'] == true) { 
					$ip = sprintf("%u", ip2long($_SERVER['REMOTE_ADDR']));
					
					$query = "SELECT code FROM " . $table_prefix . "ip2country WHERE ip_from <= '$ip' AND ip_to >= '$ip' LIMIT 1";
					$row = $SQL->selectquery($query);
					if (is_array($row)){
						$query = "SELECT country FROM  " . $table_prefix . "countries WHERE code = '" . $row['code'] . "' LIMIT 1";
						$row = $SQL->selectquery($query);
						$country = ucwords(strtolower($row['country']));
					}
					else {
						$country = 'Unavailable';
					}
				}
				
				if ($_SETTINGS['SMTP'] == true) {
					ini_set('SMTP', $_SETTINGS['SMTPSERVER']);
					ini_set('smtp_port', $_SETTINGS['SMTPPORT']);
					ini_set('sendmail_from', $_SETTINGS['SMTPEMAIL']);
				}

				// Determine EOL
				$server = strtoupper(substr($_SERVER['OS'], 0, 3));
				if ($server == 'WIN') { 
					$eol = "\r\n"; 
				} elseif ($server == 'MAC') { 
					$eol = "\r"; 
				} else { 
					$eol = "\n"; 
				}

				# Boundry for marking the split & Multitype Headers 
				$mime_boundary = md5(time());
				$subject = '=?UTF-8?B?' . base64_encode($_SETTINGS['NAME'] . ' Offline Message') . '?=';
								
				$headers = 'From: "=?UTF-8?B?' . base64_encode(html_entity_decode($name, ENT_NOQUOTES, 'UTF-8')) . '?=" <' . $_SETTINGS['EMAIL'] . '>' . $eol;
				$headers .= 'Reply-To: <' . $email . '>' . $eol;
				$headers .= 'Return-Path: <' . $email . '>' . $eol;
				$headers .= 'MIME-Version: 1.0' . $eol; 
				$headers .= 'Content-Type: multipart/alternative; boundary="' . $mime_boundary . '"' . $eol; 
				
				$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
				$message = preg_replace("/(\r\n|\r|\n)/", '<br/>', $message);
				
				$html = <<<END
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--

div, p {
	font-family: Calibri, Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
	color: #000000;
}

//-->
</style>
</head>

<body>
<p><img src="{$_SETTINGS['URL']}/livehelp/locale/en/images/OfflineEmail.gif" width="531" height="79" alt="Offline Message" /></p>
<p><strong>Message:</strong></p>
<p>$message</p>
<p>$name<br/>$email</p>
<p>&nbsp;</p>
<p><strong>IP / Hostname Logged:</strong> $hostname<br />
<strong>Country:</strong> $country<br />
<strong>Current Page:</strong> <a href="$url">$url</a><br />
<strong>Current Page Title:</strong> $title<br />
<strong>Referer:</strong> <a href="$referrer">$referrer</a></p>
<p><img src="{$_SETTINGS['URL']}/livehelp/locale/en/images/LogoSmall.png" width="217" height="52" alt="stardevelop.com" /></p>
</body>
</html>
END;

				// Add Plain Text Email
				$body = '--' . $mime_boundary . $eol;
				$body .= 'Content-type: text/plain; charset=utf-8' . $eol . $eol;
				$body .= $message . $eol . $eol;
				$body .= "IP / Hostname Logged:  $hostname" . $eol;
				if ($_SETTINGS['IP2COUNTRY'] == true) { $body .= "Country:  $country" . $eol; }
				$body .= "URL:  $url" . $eol;
				$body .= "URL Title:  $title" . $eol;
				$body .= "Referrer:  $referrer" . $eol . $eol;
				
				// Add HTML Email
				$body .= '--' . $mime_boundary . $eol;
				$body .= 'Content-type: text/html; charset=utf-8' . $eol . $eol;
				$body .= $html . $eol . $eol;
				$body .= "--" . $mime_boundary . "--" . $eol . $eol;
				
				$sendmail_path = ini_get('sendmail_path');
				mail($_SETTINGS['EMAIL'], $subject, $body, $headers);
				
				if ($bcc == true) {
				
					$bcchtml = <<<END
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--

div, p {
	font-family: Calibri, Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
	color: #000000;
}

//-->
</style>
</head>

<body>
<p><img src="{$_SETTINGS['URL']}/livehelp/locale/en/images/OfflineEmail.gif" width="531" height="79" alt="Offline Message" /></p>
<p><strong>Message:</strong></p>
<p>$message</p>
<p>$name<br/>$email</p>
<p><img src="{$_SETTINGS['URL']}/livehelp/locale/en/images/LogoSmall.png" width="217" height="52" alt="stardevelop.com" /></p>
</body>
</html>
END;

					$headers = 'From: "=?UTF-8?B?' . base64_encode(html_entity_decode($name, ENT_NOQUOTES, 'UTF-8')) . '?=" <' . $_SETTINGS['EMAIL'] . '>' . $eol;
					$headers .= 'Reply-To: <' . $_SETTINGS['EMAIL'] . '>' . $eol;
					$headers .= 'Return-Path: <' . $_SETTINGS['EMAIL'] . '>' . $eol;
					$headers .= 'MIME-Version: 1.0' . $eol; 
					$headers .= 'Content-Type: multipart/alternative; boundary="' . $mime_boundary . '"' . $eol; 

					// Add Plain Text Email
					$body = '--' . $mime_boundary . $eol;
					$body .= 'Content-type: text/plain; charset=utf-8' . $eol . $eol;
					$body .= $message . $eol . $eol;
					
					// Add HTML Email
					$body .= '--' . $mime_boundary . $eol;
					$body .= 'Content-type: text/html; charset=utf-8' . $eol . $eol;
					$body .= $bcchtml . $eol . $eol;
					$body .= "--" . $mime_boundary . "--" . $eol . $eol;

					mail($email, $subject, $body, $headers);

				}
			}
		}
	}
	
	$message = stripslashes($_REQUEST['MESSAGE']);
	
}
header('Content-type: text/html; charset=utf-8');

if (file_exists('./locale/' . LANGUAGE_TYPE . '/guest.php')) {
	include('./locale/' . LANGUAGE_TYPE . '/guest.php');
}
else {
	include('./locale/en/guest.php');
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title><?php echo($_SETTINGS['NAME']); ?></title>
<link href="styles/styles.php" rel="stylesheet" type="text/css"/>
<style type="text/css">
<!--
.background {
	background-image: url(./locale/<?php echo(LANGUAGE_TYPE); ?>/images/background.gif);
	background-repeat: no-repeat;
	background-position: center top;
	margin-left: 0px;
	margin-top: 0px;
	text-align: center;
	min-width: 600px;
}
form {
	margin:0px;
	padding:10px;
}
-->
</style>
<script language="JavaScript" type="text/javascript">
<!--

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//-->
</script>
<script language="JavaScript" type="text/JavaScript">
<!--

function getLayer(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	} else if (document.layers && document.layers[id] != null) {
		return document.layers[id];
	} else if (document.all) {
		return document.all[id];
	}
}

function showError(id) {
	var obj = getLayer(id);
	if (obj != null) {
		obj.style.visibility = 'visible';
		obj.src = 'images/errorsmall.gif';
		return true;
	}
}

function hideError(id) {
	var obj = getLayer(id);
	if (obj != null) { obj.style.visibility = 'hidden';	}
}

function validateField(field, id) {
	if (field.value == '') {
		return showError(id);
	} else {
		hideError(id);
		return false;
	}
}

function validateForm() {

	if (validateField(document.OfflineMessageForm.NAME, 'NameError') || validateField(document.OfflineMessageForm.EMAIL, 'EmailError') || validateField(document.OfflineMessageForm.MESSAGE, 'MessageError')) {
		return false;
	}
	return true;
}

//-->
</script>
</head>
<body bgcolor="<?php echo($_SETTINGS['BACKGROUNDCOLOR']); ?>" text="<?php echo($_SETTINGS['FONTCOLOR']); ?>" link="<?php echo($_SETTINGS['LINKCOLOR']); ?>" vlink="<?php echo($_SETTINGS['LINKCOLOR']); ?>" alink="<?php echo($_SETTINGS['LINKCOLOR']); ?>" class="background">
<div style="margin:0 auto; text-align:left; width:600px;">
<?php
	if ($_SETTINGS['LOGO'] != '') {
?>
<img src="<?php echo($_SETTINGS['LOGO']); ?>" alt="<?php echo($_SETTINGS['NAME']); ?>" border="0" style="position: relative; top: 15px; left: 15px;"/>
<?php
	}
	if($_REQUEST['COMPLETE'] == '' || $error != '' || $invalid_email != '' || $invalid_security != '') {
?>
<img src="/livehelp/locale/<?php echo(LANGUAGE_TYPE); ?>/images/background_offline.gif" style="position: relative; right: -90px; top: 75px;" alt="Customer Service"/>
<div align="center">
  <form action="offline.php" method="post" onsubmit="return validateForm();" name="OfflineMessageForm" id="OfflineMessageForm">
  <div style="position: relative; top: 10px;" class="offline">
    <table border="0" align="center" cellpadding="2" cellspacing="2">
      <tr>
        <td>&nbsp;</td>
        <td colspan="2" valign="bottom"><div align="center"><?php echo($unfortunately_offline_label); ?><br/>
            <?php echo($fill_details_below_label); ?>: </div></td>
      </tr>
      <?php
	  if ($invalid_email != '' || $error == true) {
	  ?>
      <tr>
        <td>&nbsp;</td>
        <td colspan="2" valign="bottom"><div align="center"><strong><?php echo($invalid_email_error_label); ?></strong></div></td>
      </tr>
      <?php
	  }
	  elseif ($invalid_security != '') {
	  ?>
      <tr>
        <td>&nbsp;</td>
        <td colspan="2" valign="bottom"><div align="center"><strong>Invalid security code entered.</strong></div></td>
      </tr>
      <?php
	  }
	  elseif ($_SETTINGS['OFFLINEEMAILREDIRECT'] != '' || $_SETTINGS['OFFLINEEMAIL'] == false) {
	  ?>
      <tr>
        <td>&nbsp;</td>
        <td colspan="2" valign="bottom"><div align="center"><strong>This feature has been disabled, please contact us via. email.  Thank you.</strong></div></td>
      </tr>
      <?php
	  }
	  ?>
      <tr>
        <td>&nbsp;</td>
        <td valign="middle"><div align="right"><?php echo($your_name_label); ?>:</div></td>
        <td><input name="NAME" type="text" id="NAME" value="<?php echo($name); ?>" size="40" style="width: 400px;" onblur="validateField(this, 'NameError')" onkeypress="return true; validateField(this, 'NameError')" <?php if ($_SETTINGS['OFFLINEEMAILREDIRECT'] != '' || $_SETTINGS['OFFLINEEMAIL'] == false) { echo('disabled="disabled"'); } ?>/>
          <img id="NameError" style="visibility: hidden" src="images/errorsmall.gif" alt="Required" width="16" height="16"/></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td valign="middle"><div align="right"><?php echo($your_email_label); ?>:</div></td>
        <td><input name="EMAIL" type="text" id="EMAIL" value="<?php echo($email); ?>" size="40" style="width: 400px;" onblur="validateField(this, 'EmailError')" onkeypress="return true; validateField(this, 'EmailError')" <?php if ($_SETTINGS['OFFLINEEMAILREDIRECT'] != '' || $_SETTINGS['OFFLINEEMAIL'] == false) { echo('disabled="disabled"'); } ?>/>
          <img id="EmailError" style="visibility: hidden" src="images/errorsmall.gif" alt="Required" width="16" height="16"/></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td valign="top"><div align="right"><?php echo($message_label); ?>:</div></td>
        <td align="right" valign="top"><div align="left">
            <textarea name="MESSAGE" cols="30" rows="5" id="MESSAGE" style="width :400px; vertical-align: middle; font-family:<?php echo($_SETTINGS['CHATFONT']); ?>; font-size: <?php echo($_SETTINGS['CHATFONTSIZE']); ?>;" onblur="validateField(this, 'MessageError')" onkeypress="return true; validateField(this, 'MessageError')" <?php if ($_SETTINGS['OFFLINEEMAILREDIRECT'] != '' || $_SETTINGS['OFFLINEEMAIL'] == false) { echo('disabled="disabled"'); } ?>><?php echo($message); ?></textarea>
            <img id="MessageError" style="visibility: hidden" src="images/errorsmall.gif" alt="Required" width="16" height="16"/></div></td>
      </tr>
	  <?php
	  if ($_SETTINGS['SECURITYCODE'] == true && (function_exists('imagepng') || function_exists('imagejpeg')) && function_exists('imagettftext') && $security_code) {
	  ?>
      <tr>
        <td>&nbsp;</td>
        <td align="right" valign="middle"><?php echo($security_code_label); ?>:</td>
        <td align="left" valign="middle"><span style="height: 30px; vertical-align: middle;"><input name="SECURITY" type="text" id="SECURITY" value="" size="6" style="width:100px;" onblur="validateField(this, 'SecurityError')" onkeypress="return true; validateField(this, 'SecurityError')" <?php if ($_SETTINGS['OFFLINEEMAILREDIRECT'] != '' || $_SETTINGS['OFFLINEEMAIL'] == false) { echo('disabled="disabled"'); } ?>/>
        </span><img src="security.php" style="height: 30px; vertical-align: middle;" alt="Security Code"/><img id="SecurityError" style="visibility: hidden" src="images/errorsmall.gif" alt="Required" width="16" height="16"/></td>
      </tr>
	  <?php
	  }
	  ?>
      <tr>
        <td>&nbsp;</td>
        <td colspan="2" align="right" valign="top"><div align="center">
            <input name="BCC" type="checkbox" value="1" <?php if ($_SETTINGS['OFFLINEEMAILREDIRECT'] != '' || $_SETTINGS['OFFLINEEMAIL'] == false) { echo('disabled="disabled"'); } ?>/>
            <?php echo($send_copy_label); ?></div></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td colspan="2" align="right" valign="top"><div align="center">
            <input name="COMPLETE" type="hidden" id="COMPLETE" value="1"/>
            <input name="COOKIE" type="hidden" id="COOKIE" value="<?php echo($_REQUEST['COOKIE']); ?>"/>
			<input name="LANGUAGE" type="hidden" id="LANGUAGE" value="<?php echo(LANGUAGE_TYPE); ?>"/>
            <table border="0" cellpadding="2" cellspacing="2">
              <tr>
                <td><div align="center">
                    <input type="submit" name="Submit" value="<?php echo($send_msg_label); ?>" <?php if ($_SETTINGS['OFFLINEEMAILREDIRECT'] != '' || $_SETTINGS['OFFLINEEMAIL'] == false) { echo('disabled="disabled"'); } ?>/>
                  </div></td>
              </tr>
            </table>
            <span class="small">International Copyright &copy; 2003 - 2013 Date Sand Models Live Help All Rights Reserved</span></div></td>
      </tr>
    </table>
	</div>
  </form>
</div>
<?php
  }
  else {
  ?>
<div align="center" style="position: relative; top: 45px;" class="offline"><br><br>
  <?php echo($thank_you_enquiry_label); ?><br>
  <?php echo($contacted_soon_label); ?><br>
  <br>
  <table border="0" align="center" cellpadding="2" cellspacing="2">
    <tr>
      <td width="22">&nbsp;</td>
      <td valign="bottom"><div align="right"><?php echo($your_email_label); ?>:</div></td>
      <td width="260"><em><?php echo($email); ?></em></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td valign="bottom"><div align="right"><?php echo($your_name_label); ?>:</div></td>
      <td><em><?php echo($name); ?></em></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td valign="top"><div align="right"><?php echo($message_label); ?>:</div></td>
      <td align="right" valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="2" align="right" valign="top"><div align="center">
          <textarea name="MESSAGE" cols="30" rows="8" id="MESSAGE" style="width:450px; font-family:<?php echo($_SETTINGS['CHATFONT']); ?>; font-size:<?php echo($_SETTINGS['CHATFONTSIZE']); ?>;"><?php echo($message); ?></textarea>
        </div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="2" align="right" valign="top"><div align="center"><span class="small">International Copyright &copy; <?php echo date('Y');?> Date Sand Models Live Help All Rights Reserved</span></div></td>
    </tr>
  </table>
</div>
<?php
  }
  ?>
  </div>
</body>
</html>
